home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: nntp.coast.net!torn!news!apollo!saed
- From: saed@engn.uwindsor.ca (Saed Aryan,13325,1100,g)
- Subject: [Q] how declare a Template class as a friend
- X-Nntp-Posting-Host: apollo.engn.uwindsor.ca
- Message-ID: <Dnx660.1Ex@news.uwindsor.ca>
- Keywords: template, friend
- Sender: news@news.uwindsor.ca (Usenet)
- Reply-To: saed@engn.uwindsor.ca
- Organization: VLSI Research Group - University of Windsor
- Date: Thu, 7 Mar 1996 22:51:36 GMT
-
- Hi all,
-
- --- How do you declare a template class to be a friend of
- another non-template class?
-
- template <class T> class A {}; // the template class A<T's>
- class B { friend class A }; // won't do it, A is not a class
- template <class V> class B { friend class A<V> }; // B<V's> are not wanted
-
- The point is to declare all classes A<whatever> to be friends of class B.
- My aim is to let all classes A<whatever> access private member data of class B,
- while B may not be a parent of all classes A<whatever>.
-
- Of course I can go about this by declaring a function f() to be a friend of B,
- and have the methods of A<whatever> access B's private member data through f().
- But I don't consider this good encapsulation, since any client may use f().
-
- As a remedy to the bad encapsulation, one could also declare a method of A,
- say g(), to be a friend of B. But consequently one would ask how to declare all
- methods A<whatever>::g() to be friends of B. Friendship is namely declared
- inside B, not in the A's.
-
- If a parent class P of all the A<whatever> classes were to be a friend of B,
- then regretfully the kids, the derived classes A<whatever>, would not inherit the
- friendship. Sigh.
-
- Any suggestions?
-
- Thanks in advance, and please send a copy of your posting to my e-mail as well.
- Aryan. saed@engn.uwindsor.ca
-
-
-